package ar.com.angrymachine.hunter.utils;

import java.util.Vector;

import ar.com.angrymachine.hunter.ActivitySet;
import ar.com.angrymachine.hunter.activities.BossAltarActivity;
import ar.com.angrymachine.hunter.activities.ExploreActivity;
import ar.com.angrymachine.hunter.activities.SlumberActivity;
import ar.com.angrymachine.hunter.gameplay.Database;
import ar.com.angrymachine.hunter.gameplay.Dungeon;
import ar.com.angrymachine.hunter.gameplay.Quest;
import ar.com.angrymachine.utils.Util;

public class TreeDungeonGenerator
{
	private Dungeon currentDungeon;
	private Vector nodes = new Vector(); // all the created nodes
	private Vector deadEndNodes = new Vector(); // all the nodes with no further exploration discoveries
	
    public Dungeon generate(Quest quest)
    {
    	// this is a lucifer-type dungeon
    	// there is one kickass creature that
    	// must be killed through the defiling of its altar
    	// the altar is in the deepest end of the dungeon
    	// so there are branches and some dead-ends, and
    	// only one branch hosts the altar
    	// the creature is always available
    	
    	// decide the depth of the dungeon
    	int depth = Util.getRandom(3,4); // 3 or 4 levels deep
    	// decide the usual amount of branching per level
    	int minBranch = 1;
    	int maxBranch = 2;
    	// spawn a random generated branching dungeon
    	currentDungeon = new Dungeon(quest);
    	createDungeonTree(currentDungeon, depth, minBranch, maxBranch);
    	// to add the final altar, choose a dead end exploration node
    	ExploreActivity altarNode = (ExploreActivity) deadEndNodes.elementAt(Util.getRandom(0, deadEndNodes.size()-1));
    	altarNode.addDiscovery(new BossAltarActivity(5000), altarNode.maxExploration);
    	// add the final boss
    	currentDungeon.addActivityInTail(new SlumberActivity(Database.lucifer, 30000));
    	return currentDungeon;
    }
    
    private ActivitySet createDungeonTree(Dungeon dungeon, int depth, int minBranch, int maxBranch)
    {
    	dungeon.addActivity(createDungeonBranch(0, depth, minBranch, maxBranch));    	
    	return dungeon;
    }
    
    private ExploreActivity createDungeonBranch(int currentDepth, int maxDepth, int minBranch, int maxBranch)
    {
		// define this node
		ExploreActivity node = DungeonGenerationUtils.createRoom(currentDungeon, currentDepth);		
		if(currentDepth < maxDepth)
		{
    		// add branches to it
    		int amountOfBranches = Util.getRandom(minBranch, maxBranch);
	    	// iterate through branches
	    	for (int d = 0; d < amountOfBranches; d++)
			{
	    		ExploreActivity branch = createDungeonBranch(currentDepth+1, maxDepth, minBranch, maxBranch);
	    		// add this branch somewhere in the last half of the exploring time
	    		int time = Util.getRandom(node.maxExploration/2, node.maxExploration);
	    		node.addDiscovery(DungeonGenerationUtils.lockBehindDoor(currentDungeon, branch, 33), time);
			}
		}
		else
		{
			deadEndNodes.add(node);
		}
		nodes.add(node);
		return node;
    }        
}
